package com.ht.controller;

import com.ht.bean.Emp;
import com.ht.bean.Module;
import com.ht.service.StypetingService;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

public class intercept implements HandlerInterceptor {
    //方法访问前
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        //获取当前登录用户的权限
        HttpSession session = httpServletRequest.getSession();
        List<Module> quanxian = (List<Module>) session.getAttribute("empquan");
//        System.out.println("++++"+quanxian);
        //获取当前点击的功能链接
        String path = httpServletRequest.getRequestURI();
        path = path.replace(httpServletRequest.getContextPath(),"");
//        System.out.println(httpServletRequest.getContextPath()+"******");
//        System.out.println("path:"+path);
        String[] pat = path.split("/");
        String pathh = "";
        pathh = pat[1]+'/'+pat[2];
        boolean blh = false;//默认是有权限的
        for (Module qx:quanxian){
            System.out.println(qx.getController()+"111"+pathh);
            if(qx.getController().equals(pathh)){
                blh = true;
                break;
            }
        }
        if (!blh){//没有权限
            //重定向到没有权限的界面
            httpServletResponse.sendRedirect(httpServletRequest.getContextPath()+"/noqx.jsp");
            return false;
        }
        return true;
    }
    //访问时
    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }
    //访问后
    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
